//
//  DKHomeCircleView.m
//  DanKU
//
//  Created by 吴斌 on 2023/10/6.
//

#import "DKHomeCircleView.h"
@interface DKHomeCircleView()
@property (strong, nonatomic) CAShapeLayer *limitRemainLayer;
@property (strong, nonatomic) CAShapeLayer *limitFrozenLayer;
@property (strong, nonatomic) CAShapeLayer *limitUsedLayer;

@property (nonatomic, strong)UILabel * dk_limitValueLab;
@property (nonatomic, strong)UILabel * dk_TotalLimitValueLab;
@end
@implementation DKHomeCircleView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.clearColor;
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    self.limitRemainLayer = [CAShapeLayer layer];
    self.limitRemainLayer.strokeColor = DK_Hex(@"#746AFF").CGColor;
    self.limitRemainLayer.lineWidth = DK_IsWidthScale(8);
    self.limitRemainLayer.lineCap = kCALineCapRound;
    self.limitRemainLayer.fillColor =  UIColor.clearColor.CGColor;
    self.limitRemainLayer.cornerRadius = DK_IsWidthScale(4);
    [self.limitRemainLayer masksToBounds];
    [self.layer addSublayer:self.limitRemainLayer];
    
    self.limitFrozenLayer = [CAShapeLayer layer];
    self.limitFrozenLayer.strokeColor = DK_Hex(@"#FF54C2").CGColor;
    self.limitFrozenLayer.lineWidth = DK_IsWidthScale(8);
    self.limitFrozenLayer.lineCap = kCALineCapRound;
    self.limitFrozenLayer.fillColor =  UIColor.clearColor.CGColor;
    self.limitFrozenLayer.cornerRadius = DK_IsWidthScale(4);
    [self.limitFrozenLayer masksToBounds];
    [self.layer addSublayer:self.limitFrozenLayer];
    
    self.limitUsedLayer = [CAShapeLayer layer];
    self.limitUsedLayer.strokeColor = DK_Hex(@"#00CBFF").CGColor;
    self.limitUsedLayer.lineWidth = DK_IsWidthScale(8);
    self.limitUsedLayer.lineCap = kCALineCapRound;
    self.limitUsedLayer.fillColor =  UIColor.clearColor.CGColor;
    self.limitUsedLayer.cornerRadius = DK_IsWidthScale(4);
    [self.limitUsedLayer masksToBounds];
    [self.layer addSublayer:self.limitUsedLayer];
    
    //limit
    UILabel *dk_limitHeadLab = [[UILabel alloc] init];
    dk_limitHeadLab.textColor = DK_Hex(@"#333333");
    dk_limitHeadLab.font = DK_FONT_Medium(10);
    dk_limitHeadLab.text = @"Limit Available";
    [self addSubview:dk_limitHeadLab];
    [dk_limitHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(@(DK_IsWidthScale(33)));
        make.height.equalTo(@(DK_IsWidthScale(14)));
    }];
    self.dk_limitValueLab = [[UILabel alloc] init];
    self.dk_limitValueLab.textColor = DK_Hex(@"#333333");
    self.dk_limitValueLab.font = DK_FONT_Regular(20);
    self.dk_limitValueLab.text = @"₹ 0";
    [self addSubview:self.dk_limitValueLab];
    [self.dk_limitValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(dk_limitHeadLab.mas_bottom).offset(DK_IsWidthScale(6));
        make.height.equalTo(@(DK_IsWidthScale(28)));
    }];
    
    //total limit
    UIImageView* dk_TotalLimitBgImgView = [[UIImageView alloc] init];
    dk_TotalLimitBgImgView.userInteractionEnabled = YES;
    dk_TotalLimitBgImgView.image = DK_GetImage(@"dk_HomeTotalLimitBg");
    [self addSubview:dk_TotalLimitBgImgView];
    [dk_TotalLimitBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    self.dk_TotalLimitValueLab = [[UILabel alloc] init];
    self.dk_TotalLimitValueLab.textColor = DK_Hex(@"#333333");
    self.dk_TotalLimitValueLab.font = DK_FONT_Regular(15);
    self.dk_TotalLimitValueLab.text = @"Total Limit  ₹ 0";
    self.dk_TotalLimitValueLab.textAlignment = NSTextAlignmentCenter;
    [dk_TotalLimitBgImgView addSubview:self.dk_TotalLimitValueLab];
    [self.dk_TotalLimitValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    [self initData];
}
- (void)initData{
    self.limitRemain = @"2000";
    self.limitFrozen = @"2000";
    self.limitUserd = @"2000";
    self.limitAvailable = @"6000";
    
    [self startUpdate];
}
- (void)startUpdate{
    CGFloat limitRemainMarge = M_PI*0.05;
    CGFloat limitFrozenMarge = M_PI*0.05;
    CGFloat limitTital = fabsf([self.limitUserd floatValue]) + fabsf([self.limitRemain floatValue])+fabsf([self.limitFrozen floatValue]);
    if((abs([self.limitRemain intValue]) <= 0)&&(abs([self.limitFrozen intValue]) <= 0)&&(abs([self.limitUserd intValue]) <= 0)){
        limitRemainMarge = M_PI*0.05;
        limitFrozenMarge = M_PI*0.05;
    }else if((abs([self.limitRemain intValue]) > 0)&&(abs([self.limitFrozen intValue]) > 0)&&(abs([self.limitUserd intValue]) > 0)){
        limitRemainMarge = M_PI*0.05;
        limitFrozenMarge = M_PI*0.05;
    }else if(abs([self.limitRemain intValue]) > 0){
        if(abs([self.limitFrozen intValue]) > 0){
            limitRemainMarge = M_PI*0.05;
            limitFrozenMarge = 0;
        }else{
            if(abs([self.limitUserd intValue]) > 0){
                limitRemainMarge = 0;
                limitFrozenMarge = M_PI*0.05;
            }else{
                limitRemainMarge = 0;
                limitFrozenMarge = 0;
            }
        }
    }else if(abs([self.limitFrozen intValue]) > 0){
        if(abs([self.limitRemain intValue]) > 0){
            limitRemainMarge = M_PI*0.05;
            limitFrozenMarge = 0;
        }else{
            if(abs([self.limitUserd intValue]) > 0){
                limitRemainMarge = 0;
                limitFrozenMarge = M_PI*0.05;
            }else{
                limitRemainMarge = 0;
                limitFrozenMarge = 0;
            }
        }
    }else if(abs([self.limitUserd intValue]) > 0){
        if(abs([self.limitRemain intValue]) > 0){
            limitRemainMarge = M_PI*0.05;
            limitFrozenMarge = 0;
        }else{
            if(abs([self.limitFrozen intValue]) > 0){
                limitRemainMarge = 0;
                limitFrozenMarge = M_PI*0.05;
            }else{
                limitRemainMarge = 0;
                limitFrozenMarge = 0;
            }
        }
    }
    CGFloat limitAll = M_PI - limitRemainMarge - limitFrozenMarge;

    //limitRemain
    CGFloat limitRemainStart = M_PI;
    CGFloat limitRemainEnd = limitRemainStart + limitAll *(fabsf([self.limitRemain floatValue])/limitTital);
    //limitFrozen
    CGFloat limitFrozenStart = limitRemainEnd+limitRemainMarge;
    CGFloat limitFrozenEnd = limitFrozenStart + limitAll *(fabsf([self.limitFrozen floatValue])/limitTital);
    //limitUsed
    CGFloat limitUsedStart = limitFrozenEnd+limitFrozenMarge;
    CGFloat limitUsedEnd = M_PI *2;
    
    UIBezierPath *limitRemainPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(DK_IsWidthScale(92), DK_IsWidthScale(84)) radius:DK_IsWidthScale(80) startAngle:limitRemainStart endAngle:limitRemainEnd clockwise:YES];
    self.limitRemainLayer.path = limitRemainPath.CGPath;

    UIBezierPath *limitFrozenPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(DK_IsWidthScale(92), DK_IsWidthScale(84)) radius:DK_IsWidthScale(80) startAngle:limitFrozenStart endAngle:limitFrozenEnd clockwise:YES];
    self.limitFrozenLayer.path = limitFrozenPath.CGPath;
    
    UIBezierPath *limitUsedPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(DK_IsWidthScale(92), DK_IsWidthScale(84)) radius:DK_IsWidthScale(80) startAngle:limitUsedStart endAngle:limitUsedEnd clockwise:YES];
    self.limitUsedLayer.path = limitUsedPath.CGPath;
}
-(void)setLimitAvailable:(NSString *)limitAvailable{
    _limitAvailable = limitAvailable;
    self.dk_limitValueLab.text = [NSString stringWithFormat:@"₹ %@",[NSString makeShowMoney:_limitAvailable]];
}
-(void)setTotalLimitValue:(NSString *)totalLimitValue{
    _totalLimitValue = totalLimitValue;
    self.dk_TotalLimitValueLab.text = [NSString stringWithFormat:@"Total Limit  ₹ %@",[NSString makeShowMoney:_totalLimitValue]];
}
@end
